package view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JOptionPane;

import beans.Professor;

public class ManterProfessorGUIImpl implements IManterProfessorView {
	private ActionListener listener = null;
	private Professor professor = null;
	private String action = null;
	
	private JFrame frPrincipal = null;
	private JPanel pPrincipal = null;
	private JPanel pNomeProfessor = null;
	private JPanel pSenhaProfessor = null;
	private JPanel pButtons = null;
	private JTextField fNomeProfessor = null;
	private JTextField fSenhaProfessor = null;
	private JButton bOk = null;
	private JButton bCancelar = null;
	private String descr = null;
	private JLabel lDescricao = null;
	
	public ManterProfessorGUIImpl(JFrame fmPrincipal) {
		buildGUI(frPrincipal);
	}
	
	public ManterProfessorGUIImpl(JFrame frPrincipal, Professor professor) {
		this.professor = professor;
		buildGUI(frPrincipal);
	}

	public void setActionEventListener(ActionListener listener) {
		this.listener = listener;
	}

	public void show() throws Exception {
		//frPrincipal.pack();
		frPrincipal.setSize(500, 180);
		frPrincipal.setVisible(true);
	}

	public void showError(Exception e) {
		JOptionPane.showMessageDialog(frPrincipal, e.toString(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
	}

	public Professor getProfessor() {
		return professor;
	}

	public void setProfessor(Professor professor) {
		this.professor = professor;
	}

	private void buildGUI(JFrame frPrincipal) {
		this.frPrincipal = frPrincipal;
		pPrincipal = new JPanel();
		pNomeProfessor = new JPanel();
		fNomeProfessor = new JTextField(20);
		pSenhaProfessor = new JPanel();
		fSenhaProfessor = new JTextField(20);
		bOk = new JButton("OK");
		bCancelar = new JButton("Cancelar");
		pButtons = new JPanel();
		lDescricao = new JLabel();
		
		refresh();
		
		bOk.addActionListener(new OkActionListener());
		bCancelar.addActionListener(new CancelarActionListener()); 
		
		pButtons.setLayout(new FlowLayout());
		pButtons.add(bOk);
		pButtons.add(bCancelar);
		
		pNomeProfessor.setLayout(new FlowLayout());
		pNomeProfessor.add(new JLabel("Nome do Professor:"));
		pNomeProfessor.add(fNomeProfessor);
		
		pSenhaProfessor.setLayout(new FlowLayout());
		pSenhaProfessor.add(new JLabel("Senha do Professor:"));
		pSenhaProfessor.add(fSenhaProfessor);
		
		
		pPrincipal.setLayout(new BoxLayout(pPrincipal, BoxLayout.Y_AXIS));
		pPrincipal.add(lDescricao);
		pPrincipal.add(pNomeProfessor);
		pPrincipal.add(pSenhaProfessor);
		
		frPrincipal.getContentPane().add(BorderLayout.CENTER, pPrincipal);
		frPrincipal.getContentPane().add(BorderLayout.SOUTH, pButtons);
	}
	
	class OkActionListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			// atualizar os dados do professor
			professor.setNome(fNomeProfessor.getText());
			professor.setSenha(fSenhaProfessor.getText());
			listener.actionPerformed(new ActionEvent(getView(), Professor.ID_PROFESSOR, action));
			//professor = new Professor();
			refresh();
		}
	}
	
	class CancelarActionListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			listener.actionPerformed(new ActionEvent(getView(), Professor.ID_PROFESSOR, Professor.ACT_CANCEL));
		}
	}
	
	private IManterProfessorView getView() {
		return this;
	}

	public void refresh() {
		if (professor.getIdentificacao() == 0) {
			action = Professor.ACT_ADD;
			descr = "Cadastrar novo professor";
			fNomeProfessor.setText("");
			fSenhaProfessor.setText("");
		}
		else {
			action = Professor.ACT_UPDATE;
			descr = "Atualizar professor";
			fNomeProfessor.setText(professor.getNome());
	}
		lDescricao.setText(descr);
	}

	public void hide() {
		frPrincipal.setVisible(false);
		
	}
}
